package kr.co.doriclassic.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import kr.co.doriclassic.mylibrary.ComboPanel;
import kr.co.doriclassic.mylibrary.ConnectionFactory;
import kr.co.doriclassic.mylibrary.InputPanel;
import kr.co.doriclassic.summary_java.dao.EmployeeDao;
import kr.co.doriclassic.summary_java.dao.InterfaceDao;
import kr.co.doriclassic.summary_java.dto.Employee;

public class PanelEmployeetUI extends JPanel implements ActionListener {
	private JButton btnInsert;
	private JButton btnDelete;
	private JButton btnUpdate;
	private InputPanel pEmpNo;//라이브러리 패널
	private InputPanel pEmpName;
	private ComboPanel pTitle;
	private ComboPanel pManager;
	private InputPanel pSalary;
	private ComboPanel pDept;	
	EmployeeDao dao;
	private Employee emp;
	private JPanel panel_top;
	private JLabel lblNewLabel;
	private JPanel panel;
	
	private HashMap<String, Integer> titleHash;
	private HashMap<String, Integer> managerHash;
	private HashMap<String, Integer> departmentHash;
	
	
	public PanelEmployeetUI() {
		initialize();
		btnUpdate.setVisible(false);
		btnDelete.setVisible(false);
		this.setLblNewLabel("사원정보추가");
		dao=new EmployeeDao(ConnectionFactory.getConnection());	
		insertCombo();		
	}
	public PanelEmployeetUI(Employee emp, String title, String manager, String dept) {
		initialize();
		this.emp = emp;
		btnInsert.setVisible(false);
		pEmpNo.setTf(emp.getEmpNo()+""); 
		pEmpNo.editableTf(false);
		pEmpName.setTf(emp.getEmpName()); 
		pSalary.setTf(emp.getSalary()+"");
		this.setLblNewLabel("사원정보수정");
		insertCombo();
		pTitle.setSelectedCb(title);
		pManager.setSelectedCb(manager);
		pDept.setSelectedCb(dept);		
	}
	private void insertCombo() {
		dao=new EmployeeDao(ConnectionFactory.getConnection());			
		try {
			titleHash = dao.searchTitleComboBox();
			Set<String> titleArr= titleHash.keySet();
			String[] titleStr = new String[titleHash.size()];
			titleArr.toArray(titleStr);
			pTitle.addCb(titleStr);
			
			managerHash = dao.searchManagerComboBox();
			Set<String> managerArr= managerHash.keySet();
			String[] managerStr = new String[managerHash.size()];
			managerArr.toArray(managerStr);
			pManager.addCb(managerStr);
			
			departmentHash = dao.searchDeptComboBox();
			Set<String> departmentArr= departmentHash.keySet();
			String[] departmentStr = new String[departmentHash.size()];
			departmentArr.toArray(departmentStr);
			pDept.addCb(departmentStr);
		} catch (SQLException e) {
			e.printStackTrace();
		}			
	}
	public InterfaceDao<Employee> getDao() {
		return dao;
	}
	
	public void setDao(EmployeeDao dao) {
		this.dao = dao;
	}
	public String getLblNewLabel() {
		return lblNewLabel.getText();
	}

	public void setLblNewLabel(String s) {
		lblNewLabel.setText(s);
	}
		
	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		panel_top = new JPanel();
		add(panel_top, BorderLayout.NORTH);

		lblNewLabel = new JLabel("New label");
		panel_top.add(lblNewLabel);

		JPanel panel_center = new JPanel();
		add(panel_center, BorderLayout.CENTER);
		panel_center.setLayout(new GridLayout(0, 1, 0, 0));
		
		pEmpNo = new InputPanel("사원번호");
		panel_center.add(pEmpNo);
		
		pEmpName = new InputPanel("사원명");
		panel_center.add(pEmpName);
		
		pTitle = new ComboPanel("직책");
		panel_center.add(pTitle);
		
		pManager = new ComboPanel("매니저");
		panel_center.add(pManager);
		
		pSalary = new InputPanel("급여");
		panel_center.add(pSalary);
		
		pDept = new ComboPanel("부서명");
		panel_center.add(pDept);
		

		JPanel panel_bottm = new JPanel();
		add(panel_bottm, BorderLayout.SOUTH);
		panel_bottm.setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		panel_bottm.add(panel, BorderLayout.NORTH);

		btnInsert = new JButton("추가");
		btnInsert.addActionListener(this);
		panel.add(btnInsert);
		
		btnUpdate = new JButton("수정");
		btnUpdate.addActionListener(this);
		panel.add(btnUpdate);

		btnDelete = new JButton();
		btnDelete.setText("삭제");
		panel.add(btnDelete);
		btnDelete.addActionListener(this);
	}
		
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			emp = inputInfo(true);
			try {
				dao.insertItem(emp);
				JOptionPane.showMessageDialog(this, "입력되었습니다");
				rPaint();	
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			} catch (NullPointerException e1){}
			
		} else if (e.getActionCommand().equals("수정")) {
			emp = inputInfo(true);
			try {
				dao.updateItembyNo(emp);
				JOptionPane.showMessageDialog(this, "수정되었습니다");
				rPaint();			
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			} catch (NullPointerException e1){}

		} else if (e.getActionCommand().equals("삭제")) {
			emp = inputInfo(false);
			try {
				dao.deleteItembyNo(emp);
				JOptionPane.showMessageDialog(this, "삭제되었습니다");
				rPaint();	
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			} catch (NullPointerException e1){}
		} 		
	}

	private void rPaint() {
		this.removeAll();
		PanelEmployeeTable tb = new PanelEmployeeTable();
		tb.setLblNewLabel("사원정보");
		try {
			tb.setList(dao.listItems());
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
		}
		tb.setDao(dao);
		this.add(tb);
		this.revalidate();
	}

	private Employee inputInfo(boolean b) {
		Employee temp=null;
		try {
			int empno = Integer.parseInt(pEmpNo.getTf().trim());
			if (b) {
				String empname = pEmpName.getTf().trim();
				int title = titleHash.get(pTitle.getSelectedCb());
				int manager = managerHash.get(pManager.getSelectedCb());
				int salary = Integer.parseInt(pSalary.getTf().trim());
				int deptno = departmentHash.get(pDept.getSelectedCb());
				temp = new Employee(empno, empname, title, manager, salary, deptno);
			} else {
				temp = new Employee();
				temp.setEmpNo(empno);				
			}
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "숫자만 입력 가능함");
		}		
		return temp;
	}	
}
